package net.madarco.phpsense;

import net.madarco.phpsense.util.QuercusEnviroment;

import org.eclipse.dltk.core.CompletionRequestor;
import org.eclipse.dltk.core.ISourceModule;
import org.eclipse.dltk.core.IType;
import org.eclipse.php.internal.core.codeassist.contexts.ClassMemberContext;

public class CompletionContext extends ClassMemberContext {
	private QuercusEnviroment quercusUtil;

	public CompletionContext(QuercusEnviroment quercusUtil) {
		this.quercusUtil = quercusUtil; 
	}
	
	public String[] getCompletionProposals() {
		if (quercusUtil.isGetInstanceElementsCompletionDefined()	&& getTriggerType() == Trigger.OBJECT) {
			return this.quercusUtil.callGetInstanceElementsCompletionDefined(getLhsTypes());
		}
		else if (quercusUtil.isGetClassElementsCompletionDefined()	&& getTriggerType() == Trigger.CLASS) {
			return this.quercusUtil.callGetClassElementsCompletionDefined(getLhsTypes());
		}
		return new String[0];
	}

	public boolean isValid(ISourceModule sourceModule, int offset,
			CompletionRequestor requestor) {

		// Call to super to verify that cursor is in the class member call
		// context
		if (super.isValid(sourceModule, offset, requestor)) {

			// This context only supports "->" trigger type (not the "::")
			if (quercusUtil.isGetInstanceElementsCompletionDefined()	&& getTriggerType() == Trigger.OBJECT) {

				IType[] recieverClass = getLhsTypes();

				// Complete only with know classes:
				if (recieverClass.length > 0) {
					return true;
				}
				return false;
			} else if (quercusUtil.isGetClassElementsCompletionDefined()	&& getTriggerType() == Trigger.CLASS) {
				IType[] recieverClass = getLhsTypes();

				// Complete only with know classes:
				if (recieverClass.length > 0) {
					return true;
				}
				return false;
			}
		}

		return false;
	}
}